package we.socialcontroller.zingme;

import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import we.common.CCommonHandler;
import we.socialcontroller.CSocialControllerException;
import we.socialcontroller.CSocialUserInfo;

public class ZM_User extends ZM_API_Common {
    public ZM_User(ZM_Config config) {
        super(config, "/users");
    }

    private static CSocialUserInfo parseRespone(String respone){
        try {
            CCommonHandler.trace(respone);
                
            JSONArray jsonArr = new JSONArray(respone);
            JSONObject json = jsonArr.getJSONObject(0);
            CSocialUserInfo result = new CSocialUserInfo();

            result.setFirstname(json.getString("firstname"));
            result.setGender(json.getString("gender"));
            result.setHeadurl(json.getString("headurl"));
            result.setLastname(json.getString("lastname"));
            result.setProfile_url(json.getString("profile_url"));
            result.setTinyurl(json.getString("tinyurl"));
            result.setUserid(json.getString("userid"));
            result.setUsername(json.getString("username"));
            result.setDisplayname(json.getString("displayname"));
            result.setDob(json.getString("dob"));
            result.setStatus(json.getString("status"));

            return result;
        } catch (JSONException e) {
            CCommonHandler.writeErrLog(e);
            return null;
        }

    }

    public CSocialUserInfo getUserInfo(long userId) throws CSocialControllerException {
        HashMap<String, String> args = new HashMap();
        args.put("uids", String.valueOf(userId));
        args.put("fields",
                 "userid,username,dob,status,firstname,gender,lastname,displayname,tinyurl,headurl,profile_url");
        String respone = callMethod("Users.getInfo", args);
        return parseRespone(respone);
    }
}
